package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.GgMain;
import cn.com.yx.ms.domain.gg.GgMainModel;
import cn.com.yx.ms.domain.program.CatalogModel;
import cn.com.yx.ms.service.IGgService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 公告管理
 */
@RestController
@RequestMapping(value = "/")
@Api(value = "公告管理", description = "获取或操作公告目录、公告资源")
public class GgController {

    private static final Logger logger = LoggerFactory.getLogger(GgController.class);

    @Resource
    private MessageSource messageSource;
    @Resource
    private IGgService iGgService;
    @Resource
    private RedisTemplate redisTemplate;

    // region << 公告目录 >>
    @PostMapping("gg/catalog")
    @ApiOperation(value = "创建公告目录信息", notes = "创建公告目录信息")
    @ApiImplicitParam(paramType = "body", name = "catalogModel", dataType = "Map", required = true, value = "公告目录实体详细信息")
    public ApiResult createCatalog(@RequestBody @Validated CatalogModel catalogModel, BindingResult bindingResult) {
        return iGgService.saveGgCatalog(catalogModel);
    }

    @DeleteMapping("gg/catalogs/{id}")
    @ApiOperation(value = "删除公告目录信息", notes = "删除公告目录信息")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "公告目录id")
    public ApiResult deleteCatalog(@PathVariable("id") String id) {
        //return new ApiResult<>(AppConstant.MSG_SUCCESS, iGgService.deleteGgCatalog(id));
        return iGgService.deleteGgCatalog(id);
    }

    @PutMapping("gg/catalog")
    @ApiOperation(value = "更新公告目录信息", notes = "更新公告目录信息")
    @ApiImplicitParam(paramType = "body", name = "catalogModel", dataType = "Map", required = true, value = "公告目录信息")
    public ApiResult updateCatalogModel(@RequestBody @Validated CatalogModel catalogModel, BindingResult bindingResult) {
        return iGgService.saveGgCatalog(catalogModel);
    }

    @PatchMapping("gg/catalog")
    @ApiOperation(value = "公告目录信息部分更新", notes = "公告目录信息部分更新")
    @ApiImplicitParam(paramType = "body", name = "catalogModel", dataType = "Map", required = true, value = "公告目录信息")
    public ApiResult updateCatalogProperty(@RequestBody @Validated CatalogModel catalogModel, BindingResult bindingResult) {
        //return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "更新成功", iTestService.update(testModel));
        return null;
    }


    @ResponseBody
    @RequestMapping(value = "gg/catalogs", method = RequestMethod.GET)
    @ApiOperation("获取所有公告目录信息")
    public ApiResult findCatalog(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        query.getConditions().put("catalogname", request.getParameter("catalogname"));
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                "",
                iGgService.findGgCatalogs(query));

    }


    @ResponseBody
    @RequestMapping(value = "gg/catalogs/{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "公告目录ID")
    @ApiOperation("获取单个公告目录信息")
    public ApiResult findCatalogById(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iGgService.findGgCatalog(id));
    }

    // endregion

    // region << 公告管理 >>
    @PostMapping("gg/main")
    @ApiOperation(value = "创建公告信息", notes = "创建公告信息")
    @ApiImplicitParam(paramType = "body", name = "userModel", dataType = "Map", required = true, value = "公告实体详细信息")
    public ApiResult create(@RequestBody @Validated GgMainModel ggMainModel, BindingResult bindingResult) throws Exception {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iGgService.saveGgMain(ggMainModel));
    }

    @DeleteMapping("gg/mains/{id}")
    @ApiOperation(value = "删除公告信息", notes = "删除公告信息")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "用户id")
    public ApiResult delete(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iGgService.deleteGgMain(id));
    }

    @PutMapping("gg/main")
    @ApiOperation(value = "更新公告信息", notes = "更新公告信息")
    @ApiImplicitParam(paramType = "body", name = "orgModel", dataType = "Map", required = true, value = "公告实体详细信息")
    public ApiResult updateModel(@RequestBody @Validated GgMainModel ggMainModel, BindingResult bindingResult) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iGgService.saveGgMain(ggMainModel));
    }

    @PatchMapping("gg/main")
    @ApiOperation(value = "公告信息部分更新", notes = "公告信息部分更新")
    @ApiImplicitParam(paramType = "body", name = "ggModel", dataType = "Map", required = true, value = "公告信息")
    public ApiResult updateGgMain(@RequestBody @Validated GgMainModel ggMainModel, BindingResult bindingResult) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iGgService.saveGgMain(ggMainModel));
    }

    @ResponseBody
    @RequestMapping(value = "gg/mains", method = RequestMethod.GET)
    @ApiOperation("获取所有公告信息")
    public ApiResult findGg(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        query.getConditions().put("title", request.getParameter("title"));
        query.getConditions().put("catalogid", request.getParameter("catalogid"));
//        query.getConditions().put("starttime", request.getParameter("starttime"));
//        query.getConditions().put("endtime", request.getParameter("endtime"));

        return new ApiResult<>(AppConstant.MSG_SUCCESS, iGgService.findGgMains(query));
    }

    @ResponseBody
    @RequestMapping(value = "gg/main/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的公告信息")
    public ApiResult findPage(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        // 查询条件接受
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("title", request.getParameter("title"));
        query.getConditions().put("catalogid", request.getParameter("catalogid"));

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<GgMain> page = (Page<GgMain>) iGgService.findGgMains(query);
        ApiPager<GgMain> apiPager = new ApiPager<GgMain>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @ResponseBody
    @RequestMapping(value = "gg/mains/{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "用户ID")
    @ApiOperation("获取单个公告信息")
    public ApiResult findByGgId(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iGgService.findGgMain(id));

    }

    // endregion

}